package com.aiyige.page.my.order.refund;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.aiyige.R;
import com.aiyige.arouter.Router;
import com.aiyige.base.BaseActivity;
import com.aiyige.base.api.JsonUtil;
import com.aiyige.model.AgreementBackup;
import com.aiyige.model.OrderEntity;
import com.aiyige.model.Subject;
import com.aiyige.model.moment.entity.MajorCourse;
import com.aiyige.utils.GlideUtil;
import com.aiyige.utils.ListUtil;
import com.aiyige.utils.widget.LabelImageView;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 退款Base页，默认处理复用的布局和数据
 */
public abstract class RefundBaseActivity extends BaseActivity {

    @BindView(R.id.iv_cover)
    LabelImageView labelImageView;

    @BindView(R.id.tv_content_count)
    TextView tvContentCount;

    @BindView(R.id.rl_refund_content)
    RelativeLayout rlContent;

    @BindView(R.id.tv_title)
    TextView tvTitle;

    @BindView(R.id.tv_count)
    TextView tvCount;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayoutResourceId());
        ButterKnife.bind(this);
        initHeaderView();
    }

    protected abstract OrderEntity getOrderEntity();

    protected abstract int getLayoutResourceId();

    protected void initHeaderView() {
        if (getOrderEntity() == null) {
            return;
        }

        if (getOrderEntity().getGoodsType().equals(Subject.video_course) || (getOrderEntity().getGoodsType().equals(Subject.video))) {
            tvContentCount.setVisibility(getOrderEntity().getVideoStages() > 0 ? View.VISIBLE : View.INVISIBLE);
            tvContentCount.setText(getString(R.string.content_count_value, getOrderEntity().getVideoStages() + ""));
        }
        else if(getOrderEntity().getGoodsType().equals(Subject.major_course))
        {
            AgreementBackup agreementBackup = ListUtil.isEmpty(getOrderEntity().getAgreementBackup()) ? null : getOrderEntity().getAgreementBackup().get(0);
            if (agreementBackup != null && !TextUtils.isEmpty(agreementBackup.getMoreBackup())) {
                MajorCourse majorCourse = JsonUtil.toObject(agreementBackup.getMoreBackup(), MajorCourse.class);
                if (majorCourse != null) {
                    tvContentCount.setVisibility(majorCourse.getClassCount() > 0 ? View.VISIBLE : View.INVISIBLE);
                    tvContentCount.setText(majorCourse.getClassCount() + getString(R.string.class_count));
                }
            }
        }

        if (!ListUtil.isEmpty(getOrderEntity().getCover())) {
            GlideUtil.with(this).big().loadCover(getOrderEntity().getCover().get(0)).into(labelImageView.getImageView());
        }
        labelImageView.setLabel(Subject.getSubjectName(getOrderEntity().getGoodsType()));
        tvTitle.setText(getOrderEntity().getTitle());
        tvCount.setText("x"+getOrderEntity().getNum());
        rlContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Router.start(getOrderEntity().getRouter());
            }
        });
    }
}
